package com.section4.controller;

import com.section4.entity.Student;
import com.section4.entity.UpdateStudent;
import com.section4.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
@RequestMapping("/student")
public class StudentController {


@RequestMapping("/add")
public void add(//如果形参名与实参名字一致,则会自动匹配,如果不一致 则需要@RequestParam指定对应关系
                  String name,//声明的名字要和传递的名字保持一致
                  String englishName,
                  String address,
                  @RequestParam("phoneNumber")//通过注释获取前端数据
                          String phone ) {
    System.out.println(name);
    System.out.println(englishName);
    System.out.println(address);
    System.out.println(phone);
}


    @Resource
    private StudentService studentService;

    /**
     * 使用表单提交 新增数据 后端直接使用对象接收也可以
     * 要求属性与key保持一致
     *
     * @param student 实体
     * @return 新增结果
     */
    @RequestMapping("/add01")
    public String add(Student student) {
        int flag = studentService.insert(student);
        if (flag>0){
            return "/page/success.jsp";
        }
        return "/page/fail.jsp";
    }
    @RequestMapping("/add02")
    public String update(UpdateStudent student) {
        int flag = studentService.update(student);
        if (flag>0){
            return "/page/success.jsp";
        }
        return "/page/fail.jsp";
    }
    @RequestMapping("/addOrNo")
    public String addOrNo(Student student, Model model) {
        int flag = studentService.insert(student);
        //向页面封装响应数据
        model.addAttribute("flag",flag);
        return flag>0 ? "/page/success.jsp":"/page/fail.jsp";
    }

    @RequestMapping("/useModelAndView")
    public ModelAndView useModelAndView(Student student) {
        int flag = studentService.insert(student);
        return flag>0? new ModelAndView("/page/success.jsp")
                .addObject("flag",flag)
//                .addObject("studentId",student.getSid())
//                .addObject("studentName",student.getName())
//                .addObject("studentEnglishName",student.getEnglishName())
//                .addObject("studentAddress",student.getAddress())
//                .addObject("studentPhoneNumber",student.getPhoneNumber())
//                .addObject("studentBirthday",student.getBirthday())
                :new ModelAndView("/page/fail.jsp").addObject("flag",flag);
    }


    /**
     * 使用自定义类型接收参数
     * 使用自定义类型响应ajax异步请求
     * @param student
     */
    @RequestMapping("/useOurType")
    @ResponseBody
    public Student useOurType(Student student)   {
        System.out.println(student);
        return student;
    }

    @RequestMapping("/requestHead") //@Requestheader 如果是请求头传值 则该注解必须配置
public void requestHead(@RequestHeader("name") String name){// @Requestheader 如果是请求头传值 则该注解必须配置
        System.out.println(name);
}
    @RequestMapping("/requestBody")
    //@RequestBody 如果是请求体传值 可以使用实体类去接收,其属性名字与请求体名字一致
    // 该注解依赖于jackson解析 故pom.xml中需要引入jackson的maven依赖坐标
     //使用ajax提交

    public void requestBody(@RequestBody Student student){
        System.out.println(student.getName());

    }
}
